package com.link2room.quintesson.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.BatchUtil;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.base.RoomtypeBaseParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelRmtypePictureDto;
import com.link2room.quintesson.constant.QuintessonMnsConstant;
import com.link2room.quintesson.constant.QuintessonTaskNameConstant;
import com.link2room.quintesson.constant.TaobaoConstant;
import com.link2room.quintesson.dto.param.manager.TaobaoXroomtypeManagerParamDto;
import com.link2room.quintesson.dto.result.manager.TaobaoRmtypePicDto;
import com.link2room.quintesson.dto.result.manager.TaobaoXroomtypeDto;
import com.link2room.quintesson.entity.TaobaoSroomtype;
import com.link2room.quintesson.entity.TaobaoXroomtype;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.quintesson.facade.ITaobaoXroomtypeFacadeService;
import com.link2room.quintesson.feign.ironhide.RoomtypeFeignService;
import com.link2room.quintesson.service.ITaobaoAPIService;
import com.link2room.quintesson.util.TaobaoUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sss on 2017/4/10.
 */
@Component("taobaoXroomtypeFacadeService")
public class TaobaoXroomtypeFacadeServiceImpl implements ITaobaoXroomtypeFacadeService {
    @Resource
    private ITaobaoAPIService taobaoAPIService;
    @Resource
    private ICommonService commonService;
    @Resource
    private RoomtypeFeignService roomtypeFeignService;


    /**
     * 飞猪房型列表
     * @param taobaoXroomtypeManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<TaobaoXroomtypeDto> listTaobaoXroomtype(TaobaoXroomtypeManagerParamDto taobaoXroomtypeManagerParamDto) throws Exception {
        TaobaoXroomtype queryTXR=new TaobaoXroomtype();
        queryTXR.setLhotelGroupCode(taobaoXroomtypeManagerParamDto.getLhotelGroupCode());
        queryTXR.setLhotelCode(taobaoXroomtypeManagerParamDto.getLhotelCode());
        QueryResult<TaobaoXroomtype> taobaoXhotelResult = commonService.findAllEQ(TaobaoXroomtype.class, queryTXR, taobaoXroomtypeManagerParamDto.getCurrentPage(), taobaoXroomtypeManagerParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(taobaoXhotelResult,TaobaoXroomtypeDto.class,false);
    }

    /**
     * 飞猪房型详情
     * @param baseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public TaobaoXroomtypeDto findTaobaoXroomtype(BaseParamDto baseParamDto) throws Exception {
        TaobaoXroomtype taobaoXroomtype = commonService.findOneById(TaobaoXroomtype.class, baseParamDto.getId());
        return ClassConverUtil.copyPropertiesToDto(taobaoXroomtype,TaobaoXroomtypeDto.class,false);
    }

    /**
     * 更新或保存房型列表
     * @param taobaoXroomtypeManagerParamDto
     * @throws Exception
     */
    @Override
    public List<TaobaoXroomtypeDto> saveOrUpdateTaobaoXroomtypeList(TaobaoXroomtypeManagerParamDto taobaoXroomtypeManagerParamDto) throws Exception {
        if(taobaoXroomtypeManagerParamDto.getTaobaoXroomtypeDtoList()!=null&& taobaoXroomtypeManagerParamDto.getTaobaoXroomtypeDtoList().size()>0){
            List<TaobaoXroomtype> taobaoXroomtypeList = ClassConverUtil.copyPropertiesList(taobaoXroomtypeManagerParamDto.getTaobaoXroomtypeDtoList(), TaobaoXroomtype.class);
            for(TaobaoXroomtype taobaoXroomtype:taobaoXroomtypeList){
                if(StringUtil.isBlank(taobaoXroomtype.getBedType())){
                    taobaoXroomtype.setBedType(TaobaoConstant.BED_TYPE);
                }
            }
            commonService.saveOrUpdateList(taobaoXroomtypeList);
            return ClassConverUtil.copyPropertiesListToDtoList(taobaoXroomtypeList,TaobaoXroomtypeDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXroomtypeManagerParamDto.toString());
        }
    }

    /**
     * 保存飞猪房型
     * @param taobaoXroomtypeManagerParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXroomtypeDto saveOrUpdateTaobaoXroomtype(TaobaoXroomtypeManagerParamDto taobaoXroomtypeManagerParamDto) throws Exception {
        if(taobaoXroomtypeManagerParamDto.getTaobaoXroomtypeDto()!=null){
            TaobaoXroomtype taobaoXroomtype = ClassConverUtil.copyProperties(taobaoXroomtypeManagerParamDto.getTaobaoXroomtypeDto(), TaobaoXroomtype.class);
            commonService.saveOrUpdate(taobaoXroomtype);
            return ClassConverUtil.copyPropertiesToDto(taobaoXroomtype,TaobaoXroomtypeDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXroomtypeManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪房型列表
     * @param taobaoXroomtypeManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXroomtypeList(TaobaoXroomtypeManagerParamDto taobaoXroomtypeManagerParamDto) throws Exception {
        if(taobaoXroomtypeManagerParamDto.getIds()!=null&& taobaoXroomtypeManagerParamDto.getIds().size()>0){
            List<TaobaoXroomtypeManagerParamDto> taobaoXroomtypeManagerParamDtos =new ArrayList<>();
            for(Long id: taobaoXroomtypeManagerParamDto.getIds()){
                TaobaoXroomtypeManagerParamDto tbManagerParamDto=new TaobaoXroomtypeManagerParamDto();
                tbManagerParamDto.setId(id);
                taobaoXroomtypeManagerParamDtos.add(tbManagerParamDto);
            }
            BatchUtil.beginBatchTask(QuintessonMnsConstant.QUEUE_UP_TAOBAO_XROOMTYPE, QuintessonTaskNameConstant.UP_TAOBAO_XROOMTYPE, taobaoXroomtypeManagerParamDtos);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXroomtypeManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪房型
     * @param taobaoXroomtypeManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXroomtype(TaobaoXroomtypeManagerParamDto taobaoXroomtypeManagerParamDto) throws Exception {
        TaobaoXroomtype taobaoXroomtype=commonService.findOneById(TaobaoXroomtype.class,taobaoXroomtypeManagerParamDto.getId());
        if(taobaoXroomtype!=null){
            TaobaoUtil.setTaobaoClient(taobaoXroomtypeManagerParamDto.getLhotelGroupCode());
            TaobaoXroomtypeDto taobaoXroomtypeDto=ClassConverUtil.copyPropertiesToDto(taobaoXroomtype, TaobaoXroomtypeDto.class, false);
            RoomtypeBaseParamDto roomtypeBaseParamDto =new RoomtypeBaseParamDto();
            roomtypeBaseParamDto.setLhotelGroupCode(taobaoXroomtypeDto.getLhotelGroupCode());
            roomtypeBaseParamDto.setLhotelCode(taobaoXroomtypeDto.getLhotelCode());
            roomtypeBaseParamDto.setRmtype(taobaoXroomtypeDto.getRmtype());
            List<TaobaoRmtypePicDto> taobaoRmtypePicDtos=new ArrayList<>();
            QueryResult<L2RHotelRmtypePictureDto> rmtypeBigPicsResult = roomtypeFeignService.roomtypeBigPics(roomtypeBaseParamDto.paramToMap()).getQueryResult();
            QueryResult<L2RHotelRmtypePictureDto> rmtypePicsResult = roomtypeFeignService.roomtypePics(roomtypeBaseParamDto.paramToMap()).getQueryResult();
            if(rmtypeBigPicsResult.getResultlist()!=null&&rmtypeBigPicsResult.getResultlist().size()>0){
                L2RHotelRmtypePictureDto rmtypeBigPic = rmtypeBigPicsResult.getResultlist().get(0);
                taobaoRmtypePicDtos.add(new TaobaoRmtypePicDto(rmtypeBigPic.getSrc(),DefaultValue.TRUE));
            }
            if(rmtypePicsResult.getResultlist()!=null&&rmtypePicsResult.getResultlist().size()>0) {
                for(L2RHotelRmtypePictureDto rmtypePic:rmtypePicsResult.getResultlist()){
                    taobaoRmtypePicDtos.add(new TaobaoRmtypePicDto(rmtypePic.getSrc(),DefaultValue.FALSE));
                }
            }

            taobaoXroomtypeDto.setPics(JacksonUtil.beanToJson(taobaoRmtypePicDtos));
            taobaoXroomtypeDto = taobaoAPIService.syncTaobaoXroomtype(taobaoXroomtypeDto);
            commonService.updateById(ClassConverUtil.copyProperties(taobaoXroomtypeDto,TaobaoXroomtype.class));
            if(taobaoXroomtypeDto.getTaobaoSroomtypeDto()!=null){
                TaobaoSroomtype queryTSR=new TaobaoSroomtype();
                queryTSR.setLhotelGroupCode(taobaoXroomtypeDto.getLhotelGroupCode());
                queryTSR.setLhotelCode(taobaoXroomtypeDto.getLhotelCode());
                queryTSR.setOuterRid(taobaoXroomtypeDto.getOuterRid());
                TaobaoSroomtype taobaoSroomtype = commonService.findOneEQ(TaobaoSroomtype.class, queryTSR);
                if(taobaoSroomtype==null){
                    taobaoSroomtype=new TaobaoSroomtype();
                    ClassConverUtil.copyProperties(taobaoXroomtypeDto.getTaobaoSroomtypeDto(),taobaoSroomtype);
                }else {
                    taobaoSroomtype.setSrmtype(taobaoXroomtypeDto.getTaobaoSroomtypeDto().getSrmtype());
                }
                commonService.saveOrUpdate(taobaoSroomtype);
            }
        }
        BatchUtil.doBatchTaskDone(taobaoXroomtypeManagerParamDto.getTaskMainId(),
                taobaoXroomtypeManagerParamDto.getTaskId(),
                taobaoXroomtypeManagerParamDto.getIsBatch());

    }
}
